package com.order.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.order.api.constant.BaseException;
import com.order.api.constant.MenuNodeEnum;
import com.order.api.constant.MsgEnum;
import com.order.api.dao.TblRoleDao;
import com.order.api.dao.TblRolePermissionsDao;
import com.order.api.dao.UsersDao;
import com.order.api.po.TblRole;
import com.order.api.po.TblRolePermissionsExample;
import com.order.api.po.Users;
import com.order.api.po.UsersExample;
import com.order.api.service.RoleService;
import com.order.api.util.ThreadLocalUtil;
import com.order.api.vo.request.RoleListRequest;
import com.order.api.vo.request.RolePageListRequest;
import com.order.api.vo.request.RoleSaveRequest;
import com.order.api.vo.request.RoleStatusRequest;
import com.order.api.vo.response.MenuListResponse;
import com.order.api.vo.response.RoleDetailResponse;
import com.order.api.vo.response.RoleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private TblRoleDao tblRoleDao;

    @Autowired
    private TblRolePermissionsDao tblRolePermissionsDao;

    @Autowired
    private ThreadLocalUtil threadLocalUtil;

    @Autowired
    private UsersDao usersDao;

    @Override
    public boolean saveRole(RoleSaveRequest request) {
        /**
         * 1.判断参数
         * 2.检查保存的用户名是否存在,存在抛出异常用户名已存在
         * 3.根据是否传入ID进行保存或者删除操作
         * 4.删除旧角色菜单关联信息
         * 5.判断传入的菜单ID是否为空,保存菜单关联信息
         */
        return false;
    }

    @Override
    public RoleDetailResponse roleDetail(Long roleId) {
        /**
         * 1.判断参数
         * 2.根据传入的角色ID查询出对应的角色
         * 3.把需要的数据组装返回
         */
        RoleDetailResponse roleDetailResponse = new RoleDetailResponse();
        if (roleId!=null){
            TblRole role = tblRoleDao.selectByPrimaryKey(roleId);
            if (role!=null){
                List<MenuListResponse> menus = MenuListResponse.buildTree(tblRolePermissionsDao.findMenuRoleId(roleId), MenuNodeEnum.ROOT_NODE_CODE.getCode());
                roleDetailResponse =RoleDetailResponse.builder()
                        .id(role.getId())
                        .roleName(role.getRoleName())
                        .roleGroup(role.getRoleGroup())
                        .status(role.getStatus())
                        .menus(menus)
                        .build();
            }
        }

        return roleDetailResponse;
    }

    @Override
    public boolean deleteById(Long roleId) {

        boolean flag = false;
        if (roleId != null) {
           //检查是否有用户绑定此角色
            UsersExample userExample = new UsersExample();

            List<Users> list = usersDao.selectByExample(userExample);
            if (list != null && list.size() > 0) {
                throw new BaseException(MsgEnum.ERROR_ROLE_HAVE_USER_BINDING);
            }
            //删除角色
            flag = tblRoleDao.deleteByPrimaryKey(roleId) == 1;
            if (flag) {
                //删除角色权限关联信息
                TblRolePermissionsExample example = new TblRolePermissionsExample();
                example.createCriteria().andRoleIdEqualTo(roleId);
                tblRolePermissionsDao.deleteByExample(example);
            }
        }
        return flag;
    }

    @Override
    public PageInfo<RoleResponse> findRolePageList(RolePageListRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<RoleResponse> list = tblRoleDao.findList(request.getRoleName(), request.getStatus(), request.getRoleGroup());
        return new PageInfo<>(list);
    }

    @Override
    public List<RoleResponse> findRoleList(RoleListRequest request) {
        return tblRoleDao.findList(request.getRoleName(), request.getStatus(), request.getRoleGroup());
    }

    @Override
    public boolean updateStatus(RoleStatusRequest request) {
        int count = tblRoleDao.updateByPrimaryKeySelective(TblRole.builder()
                .status(request.getStatus())
                .id(request.getId())
                .build());
        return count == 1;
    }
}
